package com.testplatform.aitestplatform.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.testplatform.aitestplatform.mapper.UserPhoneContactsMapper;
import com.testplatform.aitestplatform.domain.UserPhoneContacts;
import com.testplatform.aitestplatform.service.IUserPhoneContactsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 用户手机号存储Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-26
 */
@Service
public class UserPhoneContactsServiceImpl extends ServiceImpl<UserPhoneContactsMapper, UserPhoneContacts> implements IUserPhoneContactsService
{


    /**
     * 手机号生成器工具类
     * 生成符合中国手机号规范的11位随机号码
     */


    @Autowired
    private UserPhoneContactsMapper userPhoneContactsMapper;

    /**
     * 查询用户手机号存储
     * 
     * @param id 用户手机号存储主键
     * @return 用户手机号存储
     */
    @Override
    public UserPhoneContacts selectUserPhoneContactsById(String id)
    {
        return getById(id);
    }

    /**
     * 查询用户手机号存储列表
     * 
     * @param userPhoneContacts 用户手机号存储
     * @return 用户手机号存储
     */
    @Override
    public List<UserPhoneContacts> selectUserPhoneContactsList(UserPhoneContacts userPhoneContacts)
    {
        return userPhoneContactsMapper.selectUserPhoneContactsList(userPhoneContacts);
    }

    /**
     * 新增用户手机号存储
     * 
     * @param  用户手机号存储
     * @return 结果
     */
    @Override
    public int insertUserPhoneContacts()

    {
        UserPhoneContacts userPhoneContacts = new UserPhoneContacts();
        userPhoneContacts.setPhoneNumber(generateSinglePhoneNumber());
        return save(userPhoneContacts) ? 1 : 0;
    }

    /**
     * 修改用户手机号存储
     * 
     * @param userPhoneContacts 用户手机号存储
     * @return 结果
     */
    @Override
    public int updateUserPhoneContacts(UserPhoneContacts userPhoneContacts)
    {
        return updateById(userPhoneContacts) ? 1 : 0;
    }

    /**
     * 批量删除用户手机号存储
     * 
     * @param ids 需要删除的用户手机号存储主键
     * @return 结果
     */
    @Override
    public int deleteUserPhoneContactsByIds(String[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除用户手机号存储信息
     * 
     * @param id 用户手机号存储主键
     * @return 结果
     */
    @Override
    public int deleteUserPhoneContactsById(String id)
    {
        return removeById(id) ? 1 : 0;
    }



        // 如果确实需要使用这些前缀，请保留；否则可以删除
        private static final String[] PHONE_PREFIXES = {
                // 中国移动
                "134", "135", "136", "137", "138", "139", "147", "148", "150", "151",
                "152", "157", "158", "159", "172", "178", "182", "183", "184", "187",
                "188", "195", "197", "198",

                // 中国联通
                "130", "131", "132", "145", "146", "155", "156", "166", "171", "175",
                "176", "185", "186", "196",

                // 中国电信
                "133", "149", "153", "173", "177", "180", "181", "189", "190", "191",
                "193", "199"

        };

    /**
     * 生成单个随机手机号
     */
    public static String generateSinglePhoneNumber() {
        Random random = new Random();
        String prefix = PHONE_PREFIXES[random.nextInt(PHONE_PREFIXES.length)];
        StringBuilder phoneNumber = new StringBuilder(prefix);

        // 生成剩余8位数字
        for (int i = prefix.length(); i < 11; i++) {
            phoneNumber.append(random.nextInt(10));
        }

        return phoneNumber.toString();
    }


}
